package com.hyc.config;

import com.baidu.fsg.uid.impl.CachedUidGenerator;
import com.baidu.fsg.uid.impl.DefaultUidGenerator;
import com.baidu.fsg.uid.worker.DisposableWorkerIdAssigner;
import com.baidu.fsg.uid.worker.WorkerIdAssigner;
import com.baidu.fsg.uid.worker.dao.WorkerNodeDAO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@MapperScan({"com.baidu.fsg.uid.worker.dao","com.hyc.dao"})
public class CachedUidGeneratorConfig {

    /**
     * disposableWorkerIdAssigner的入参对象类型最好使用 WorkerIdAssigner，
     * 否则其他地方引入CGLib动态代理的时候可能会导致代理混用的问题
     *
     * @param disposableWorkerIdAssigner
     * @return
     */
//    @Bean
//    public DefaultUidGenerator defaultUidGenerator(WorkerIdAssigner workerIdAssigner) {
//        DefaultUidGenerator defaultUidGenerator = new DefaultUidGenerator();
//        defaultUidGenerator.setWorkerIdAssigner(workerIdAssigner);
//        defaultUidGenerator.setTimeBits(32);
//        defaultUidGenerator.setWorkerBits(22);
//        defaultUidGenerator.setSeqBits(9);
//        defaultUidGenerator.setEpochStr("2020-01-01");
//
//        return defaultUidGenerator;
//    }

    /**
     * disposableWorkerIdAssigner的入参对象类型最好使用 WorkerIdAssigner，
     * 否则其他地方引入CGLib动态代理的时候可能会导致代理混用的问题
     *
     * @param workerIdAssigner
     * @return
     */
    @Bean
    public CachedUidGenerator cachedUidGenerator(WorkerIdAssigner workerIdAssigner) {
        CachedUidGenerator cachedUidGenerator = new CachedUidGenerator();
        cachedUidGenerator.setWorkerIdAssigner(workerIdAssigner);
        cachedUidGenerator.setTimeBits(32);
        cachedUidGenerator.setWorkerBits(22);
        cachedUidGenerator.setSeqBits(9);
        cachedUidGenerator.setEpochStr("2020-01-01");
        cachedUidGenerator.setBoostPower(3);
        cachedUidGenerator.setScheduleInterval(60L);

        return cachedUidGenerator;
    }

    @Bean
    public WorkerIdAssigner workerIdAssigner() {
        DisposableWorkerIdAssigner workerIdAssigner = new DisposableWorkerIdAssigner();
        return workerIdAssigner;
    }
}
